import 'dart:io';

import 'package:flutter/material.dart';
import 'package:material_dialogs/dialogs.dart';
import 'package:material_dialogs/shared/types.dart';
import 'package:tracker/theme/app_theme.dart';

enum PayType {
  alipay,
  wechat,
  wechatUnion,
}

class PayDialogWidget {
  PayType payType = PayType.alipay;

  /// 购买方式
  void showPayDialog(BuildContext context, String money, {Function(PayType payType)? onConfirm}) {
    Dialogs.bottomMaterialDialog(
        context: context,
        isScrollControlled: true,
        title: '选择支付方式',
        customView: StatefulBuilder(builder: (newContext, newSetState) {
          return Container(
            padding: const EdgeInsets.only(left: 20, right: 20, top: 20),
            child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              const SizedBox(
                height: 12,
              ),
              Container(
                  alignment: Alignment.center,
                  child: Text.rich(
                    TextSpan(
                      children: [
                        TextSpan(
                            text: '¥', style: TextStyle(fontSize: 17, color: AppTheme.themeConfig.primaryTextColor)),
                        TextSpan(
                            text: '$money',
                            style: TextStyle(fontSize: 26, color: AppTheme.themeConfig.primaryTextColor)),
                      ],
                    ),
                  )),
              const SizedBox(
                height: 12,
              ),
              // 支付宝
              _buildAlipay(newSetState),
              // 微信支付
              _buildWxpay(newSetState),
            ]),
          );
        }),
        customViewPosition: CustomViewPosition.BEFORE_MESSAGE,
        actions: [
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              onConfirm?.call(payType);
            },
            // 背景色
            style: ButtonStyle(
              backgroundColor: MaterialStateProperty.all(AppTheme.themeConfig.primaryColor),
              foregroundColor: MaterialStateProperty.all(Colors.white),
            ),
            child: const Text(
              '确认支付',
              style: TextStyle(fontSize: 15),
            ),
          ),
        ]);
  }

  _buildWxpay(StateSetter newSetState) {
    // // iOS不显示微信支付
    // if (Platform.isIOS) {
    //   return Container();
    // }
    return InkWell(
      onTap: () {
        newSetState(() {
          payType = PayType.wechatUnion;
        });
      },
      child: Container(
        height: 50,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Image.asset(
                  'assets/images/pay/wxpay_logo.png',
                  width: 30,
                  height: 30,
                ),
                const SizedBox(
                  width: 10,
                ),
                const Text(
                  '微信支付',
                  style: TextStyle(fontSize: 16),
                ),
              ],
            ),
            // 单选框
            Radio(
              value: PayType.wechatUnion,
              groupValue: payType,
              onChanged: (value) {
                newSetState(() {
                  payType = value as PayType;
                });
              },
            ),
          ],
        ),
      ),
    );
  }

  InkWell _buildAlipay(StateSetter newSetState) {
    return InkWell(
      onTap: () {
        newSetState(() {
          payType = PayType.alipay;
        });
      },
      child: Container(
        height: 50,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Image.asset(
                  'assets/images/pay/alipay_logo.png',
                  width: 30,
                  height: 30,
                ),
                const SizedBox(
                  width: 10,
                ),
                const Text(
                  '支付宝支付',
                  style: TextStyle(fontSize: 16),
                ),
              ],
            ),
            // 单选框
            Radio(
              value: PayType.alipay,
              groupValue: payType,
              onChanged: (value) {
                newSetState(() {
                  payType = value as PayType;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

class PayUtils {
  /// 获取支付类型
  static int getPayType(PayType payType) {
    switch (payType) {
      case PayType.alipay:
        return 2;
      case PayType.wechat:
        return 1;
      case PayType.wechatUnion:
        return 1;
    }
  }

  /// 获取支付账号
  static String getPayAccount(PayType payType) {
    switch (payType) {
      case PayType.alipay:
        return 'APP_RSK_ALIPAY_PAY';
      case PayType.wechat:
        return 'APP_RSK_WX_PAY';
      case PayType.wechatUnion:
        return 'APP_RSK_UNION_HHMM_PAY';
    }
  }
}
